//
//  PTRecorderViewController.m
//  PocketTutor
//
//  Created by yate1996 on 15/6/8.
//  Copyright (c) 2015年 yate1996. All rights reserved.


#import "PTRecorderViewController.h"
#import "PTRecorderFrame.h"
#import "PTRecorderView.h"
#import "PTRecorderCell.h"
#import "PTRecorder.h"
#import "PTRecorderDetailViewController.h"
#import "PTRecorderChatController.h"
#import "PTRecorderEvaluationReportController.h"
@interface PTRecorderViewController () <PTRecorderButtonDelegate>
@property (nonatomic, strong) NSMutableArray *recorderFrames;
@property (nonatomic, assign) NSInteger indexPathRow;
@end

@implementation PTRecorderViewController


-(NSMutableArray *)recorderFrames
{
    if(_recorderFrames == nil)
    {
        _recorderFrames = [NSMutableArray array];
    }
    
    return _recorderFrames;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"Records";
    self.view.backgroundColor = PTColor(245, 245, 245, 1.0);
    [self setupRecoders];
    //[self.navigationController.navigationBar setBarTintColor:PTColor(232, 84, 88, 0.9)];
}

-(void)setupRecoders
{
    NSMutableArray *recorders = [NSMutableArray array];
    

    PTRecorder *recorder1 = [self setupRecordDetails:@"Finish ten Sentence" teacherName:@"Margot Chou" countDown:@"Countdown：00:00" lessonIcon:@"records1" teacherIcon:@"Image1" teacherScore:@"Score:4.5" actionOrCancel:@"Study Now"];

    
    PTRecorder *recorder2 = [self setupRecordDetails:@"Complete the Sentence" teacherName:@"Margot Chou" countDown:@"Countdown：48:28" lessonIcon:@"records2" teacherIcon:@"Image2" teacherScore:@"Score:4.5" actionOrCancel:@"Cancel"];

    
    PTRecorder *recorder3 = [self setupRecordDetails:@"Sentence Expansion" teacherName:@"Margot Chou" countDown:@"05-10" lessonIcon:@"records3" teacherIcon:@"Image4" teacherScore:@"Score:4.5" actionOrCancel:@"Read My Evaluation"];
    
    PTRecorder *recorder4 = [self setupRecordDetails:@"Finish ten Sentence" teacherName:@"Margot Chou" countDown:@"Countdown：00:00" lessonIcon:@"records1" teacherIcon:@"Image1" teacherScore:@"Score:4.5" actionOrCancel:@"Study Now"];
    
    
    PTRecorder *recorder5 = [self setupRecordDetails:@"Complete the Sentence" teacherName:@"Margot Chou" countDown:@"Countdown：48:28" lessonIcon:@"records2" teacherIcon:@"Image2" teacherScore:@"Score:4.5" actionOrCancel:@"Cancel"];
    
    
    PTRecorder *recorder6 = [self setupRecordDetails:@"Sentence Expansion" teacherName:@"Margot Chou" countDown:@"05-10" lessonIcon:@"records3" teacherIcon:@"Image4" teacherScore:@"Score:4.5" actionOrCancel:@"Read My Evaluation"];
    
    PTRecorder *recorder7 = [self setupRecordDetails:@"Finish ten Sentence" teacherName:@"Margot Chou" countDown:@"Countdown：00:00" lessonIcon:@"records1" teacherIcon:@"Image1" teacherScore:@"Score:4.5" actionOrCancel:@"Study Now"];
    
    
    PTRecorder *recorder8 = [self setupRecordDetails:@"Complete the Sentence" teacherName:@"Margot Chou" countDown:@"Countdown：48:28" lessonIcon:@"records2" teacherIcon:@"Image2" teacherScore:@"Score:4.5" actionOrCancel:@"Cancel"];
    
    
    PTRecorder *recorder9 = [self setupRecordDetails:@"Sentence Expansion" teacherName:@"Margot Chou" countDown:@"05-10" lessonIcon:@"records3" teacherIcon:@"Image4" teacherScore:@"Score:4.5" actionOrCancel:@"Read My Evaluation"];
    
    
    [recorders addObjectsFromArray:@[recorder1,recorder2,recorder3,recorder4,recorder5,recorder6,recorder7,recorder8,recorder9]];
    [self.recorderFrames addObjectsFromArray:[self recorderFramesWithRecorders:recorders]];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView reloadData];
    
}

-(PTRecorder *)setupRecordDetails:(NSString *)lessonTitle teacherName:(NSString *)teacherName  countDown:(NSString *)countDown lessonIcon:(NSString *)lessonIcon teacherIcon:(NSString *)teacherIcon teacherScore:(NSString *)teacherScore actionOrCancel:(NSString *)actionOrCancel
{
    PTRecorder *recorder = [[PTRecorder alloc]init];
    recorder.lessonTitle = lessonTitle;
    recorder.teacherName = teacherName;
    recorder.countDown = countDown;
    recorder.lessonIcon = lessonIcon;
    recorder.teacherIcon = teacherIcon;
    recorder.teacherScore = teacherScore;
    recorder.actionOrCancel = actionOrCancel;
    return recorder;
}

-(NSArray *)recorderFramesWithRecorders:(NSArray *)recorders
{
    NSMutableArray *frames = [NSMutableArray array];
    for (PTRecorder *recorder in recorders) {
        PTRecorderFrame *frame = [[PTRecorderFrame alloc]init];
        frame.recorder = recorder;
        [frames addObject:frame];
    }
    return frames;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 0.001;
    
}
-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PTRecorderCell *cell = [PTRecorderCell cellWithTableView:tableView];
    cell.recorderFrame = self.recorderFrames[indexPath.section];
    cell.backgroundColor = [UIColor whiteColor];
//    if ([cell.ac  button.titleLabel.text isEqualToString:@"Read My Evaluation"]) {
//        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//
//    }
    cell.delegate = self;
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.recorderFrames.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
//    return self.recorderFrames.count;
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PTRecorderFrame *frame = self.recorderFrames[indexPath.section];
    return frame.cellHeight;
}
-(void)didSelectedButton:(PTRecorderButtonType)buttonType UIButton:(UIButton *)button
{
    switch (buttonType) {
        case PTRecorderButtonTypeactionOrCancel:
            [self actionOrCancel:button];
            break;
            
        case PTRecorderButtonTypelessonTitle:
            [self lessonTitle];
            break;
            
        case PTRecorderButtonTypereadMyText:
            [self countDown];
            break;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.indexPathRow = indexPath.row;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

-(void)countDown
{
    PTRecorderEvaluationReportController *next = [[PTRecorderEvaluationReportController alloc]init];
    
    [self.navigationController pushViewController:next animated:YES];
}
-(void)actionOrCancel:(UIButton *)button
{
    if([button.titleLabel.text isEqualToString:@"Read My Evaluation"]) {
        PTRecorderEvaluationReportController *next = [[PTRecorderEvaluationReportController alloc]init];
        [self.navigationController pushViewController:next animated:YES];

    } else if([button.titleLabel.text isEqualToString:@"Cancel"]) {
        
    } else {
        PTRecorderChatController *next = [[PTRecorderChatController alloc]init];
        [self.navigationController pushViewController:next animated:YES];
    }
    

    
}

-(void)lessonTitle
{
    PTRecorderDetailViewController *next = [[PTRecorderDetailViewController alloc]init];
    PTRecorderFrame *frame = self.recorderFrames[self.indexPathRow];
    next.recorder = frame.recorder;
    [self.navigationController pushViewController:next animated:YES];
}
@end
